const {
  $util,
} = getApp();
Page({
  data: {
    page: 1,
    list: [],
    hasMore: true,
  },
  onLoad(opts) {
    this.setData(opts, () => {
      this.loadList();
    });
    // 书目推荐页
    wx.setNavigationBarTitle({
      title: opts.barTitle && decodeURIComponent(opts.barTitle) || '书目推荐',
    });
  },
  loadTabBarList() {
    $util.request({
      url: 'entry/wxapp/edu.databank.getTabbar',
      data: {},
      success: (res) => {
        this.setData({
          tabList: res.navlist,
        }, () => {
          this.loadList();
        });
      },
    });
  },
  loadList() {
    const D = this.data;
    $util.request({
      url: 'entry/wxapp/edu.databank.bookmore',
      data: {
        page: D.page,
        nid: 0,
      },
      success: (res) => {
        let list = D.list;
        list = list.concat(res.list);
        this.setData({
          list,
          hasMore: res.total > list.length,
          page: res.total > list.length ? ++D.page : D.page,
        });
      },
    });
  },
  onReachBottom() {
    if (this.data.hasMore) {
      this.loadList();
    }
  },
});
